package com.myex.demo.common.mq;


import com.myex.demo.model.OrderMsg;
import com.myex.demo.service.MiaoShaService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQReceiver {


    @Autowired
    MiaoShaService miaoShaService;

    @RabbitListener(queues = MQConfig.QUEUE)
    public void receive(OrderMsg orderMsg){


        //查询数据库中商品库存
        Integer stock = miaoShaService.queryGoodsStockById(orderMsg.getGoodsId());
        if (stock <= 0 || stock < Integer.parseInt(orderMsg.getNum())){
            System.out.println("用户"+orderMsg.getUserId()+"秒杀时商品的库存量不足");
            return;
        }
        //这里业务是同一用户同一商品只能购买一次,所以判断该商品用户是否下过单
//        List<OrderRecord> orders = miaoShaService.queryOrderByUserIdAndCoodsId(userId,goodsId);
//        if(orders != null && orders.size() > 0){//重复下单
//            return;
//        }
        //减库存,下订单
        miaoShaService.miaosha(orderMsg);


    }




}
